﻿using System;
using Orchard.DisplayManagement;
using Orchard.Events;
using Orchard.Localization;

namespace Hazdesign.TwitterRules.Rules {
    public interface IFormProvider : IEventHandler {
        void Describe(dynamic context);
    }

    public class PublishingForms : IFormProvider {
        protected dynamic Shape { get; set; }
        public Localizer T { get; set; }

        public PublishingForms(IShapeFactory shapeFactory){
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
        }

        public void Describe(dynamic context) {
            Func<IShapeFactory, dynamic> form = 
                shape => Shape.Form(
                Id: "ActionTweet",
                _Message: Shape.Textarea(
                    Id: "Tweet", Name: "Tweet",
                    Title: T("Tweet"),
                    Description: T("The template of the Tweet."),
                    Classes: new[] { "tokenized" }
                    )
                );
            context.Form("ActionTweet", form);
        }
    }

    public interface IFormHandler : IEventHandler {
        void Validating(dynamic context);
    }

    public class PublishingFormsValitator : IFormHandler{
        public PublishingFormsValitator(){
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public void Validating(dynamic context) {
            if (context.FormName != "ActionTweet")
                return;

            if (context.ValueProdiver.GetValue("Tweet").AttemptedValue == String.Empty) {
                context.ModelState.AddModelError("Tweet", T("You must provide a tweet template.").Text);
            }
        }
    }
}